/*
	developed by fiskus_boulder
	http://fiskus.name/
	mail@fiskus.name
*/
var BA = {
	$: function(id) {
		return document.getElementById(id);
	},

	append: function(parent, el, options) {
		var child = document.createElement(el);
		var options = options || 0;
		for (var i = 0; i < options.length; i+=2) {
			child[options[i]] = options[i+1];
		}
		parent.appendChild(child);
	},

	close: function() {
		BA.kill(BA.$(BA.shader));
		BA.kill(BA.$(BA.id));
	},

	init: function(level, rel, header, title, shader, id, img_id) {
		BA.level = level || "fit";
		BA.rel = rel || "blueant";
		BA.header = header || "h1";
		BA.title = title || "Без названия";
		BA.shader = shader || "shader";
		BA.id = id || "blueant_table";
		BA.img_id = img_id || "blueant_img";

		var all_links = BA.tag("a");
		for (var i = 0; i < all_links.length; i++) {
			if (all_links[i].rel.indexOf(BA.rel) > -1) {
				all_links[i].onclick = BA.show;
			}
		}
	},

	kill: function(element) {
		BA.tag("body")[0].removeChild(element);
	},

	resize: function() {
		var h = this.height;
		var w = this.width;
		if (this.height > window.innerHeight) {
			this.height = window.innerHeight - 100;
			if (this.width > window.innerWidth) {
				this.width = window.innerWidth - 100;
				this.height = h*(this.width/w);
			}
		}

	},

	show: function(thumb) {
		BA.append(BA.tag("body")[0], "div", ["id", BA.shader]);
		BA.append(BA.tag("body")[0], "table", ["id", BA.id, "className", BA.level]);
		BA.append(BA.$(BA.id), "tbody");
		BA.append(BA.tag("tbody", BA.$(BA.id))[0], "tr");
		BA.append(BA.tag("tr", BA.$(BA.id))[0], "td");
		BA.append(BA.tag("td", BA.$(BA.id))[0], "img", ["id", BA.img_id, "src", this.href, "onclick", BA.close]);
		if (BA.level != "base") {
			BA.tag("img", BA.$(BA.id))[0].onload = BA.resize;
			if (BA.level != "base" && BA.level != "fit") {
				var title = this.title || thumb.target.alt || BA.title;
				BA.tag("img", BA.$(BA.id))[0].alt = title;
				BA.append(BA.tag("td", BA.$(BA.id))[0], BA.header, ["innerHTML", title]);
			}
		}
		return false;
	},

	tag: function(element, parent) {
		var parent = parent || document;
		return parent.getElementsByTagName(element);
	}
}

window.onload = function() {
	BA.init("title");
}
